﻿
layui.use(['form','element','upload','table','layer'], function(){
    //直接渲染表格

    var form = layui.form;
    var table = layui.table;
    var element = layui.element;
    let upload = layui.upload;
    var $ = layui.$;
    //印刷
    table.render({
        elem: '#printingTable'
        ,headers: {
            Accept: "application/json; charset=utf-8",
        }
        ,height: 700
        ,url: '/general/testzs/public/printing/sales/SalesPrinting'
        ,cols: [[ //表头
            { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
            { title: 'printing_id', hide:true, }
            ,{field: 'printing_number', title: '申请单号'}
            ,{field: 'customer_name', title: '客户名称'}
            ,{field: 'order_number', title: '订单号'}
            ,{field: 'user_name', title: '制单人'}
            ,{field: 'create_time', title: '发单时间'}
            ,{field: 'tips', title: '当前状态'}
            ,{field:'right', title: '操作',toolbar:"#PrintingTool"}
            ,{ title: '采购状态',field:'purchase_status',  hide:true, }
            ,{ title: '总经办状态',field:'general_status', hide:true, }
            ,{ title: '质检部状态',field:'inspector_status', hide:true, }
            ,{ title: '采购审批时间',field:'purchase_time',  hide:true, }
            ,{ title: '总经办审批时间',field:'general_time', hide:true, }
            ,{ title: '质检部审批时间',field:'inspector_time', hide:true, }
            ,{ title: '采购部内容',field:'purchase_content', hide:true, }
            ,{ title: '质检部内容',field:'inspector_content', hide:true, }
        ]]
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 200 //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'count' //规定数据总数的字段名称，默认：count
            , dataName: 'data' //规定数据列表的字段名称，默认：data
        }
        ,page: true
        ,text: { none: '暂无印刷记录' }
    });

    table.on('tool(printingTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            //打开详情
            layer.open({
                type: 1,
                area: ['1000px','800px'],
                offset: '100px',
                title: data.user_name+'的印刷详情',
                content: $('#PrintingDetailHtml').html(), // 注意这里有.html()
                success: function(layero, dIndex) {
                    //查询类别 渲染上去
                    //渲染 不让操作
                    $('#customer_name_view').val(data.customer_name); //data.printing_number
                    $('#order_number_view').val(data.order_number); //data.printing_number
                    $('#remarks_view').val(data.remarks);
                    $('#purchase_content_view').val(data.purchase_content); //data.printing_number
                    $('#inspector_content_view').val(data.inspector_content);

                    $("#printing_price_view").val(data.printing_price);
                    $("#currency_view").val(data.currency);
                    $("input[name='is_collect_cost'][value='"+ data.is_collect_cost +"']").prop("checked", "checked");

                    //审批下列
                    form.render();
                    //审批下列
                    $('#user_name').val(data.user_name); //data.printing_number
                    $('#create_time').val(data.create_time); //data.printing_number

                    if (data.purchase_status === 2){
                        $('#purchase_tips').val('未审批');
                    }else if (data.purchase_status === 1){
                        $('#purchase_tips').val('通过');
                        $('#purchase_time').val(data.purchase_time);
                    }else if (data.purchase_status === 3){
                        $('#purchase_tips').val('拒绝');
                        $('#purchase_time').val(data.purchase_time);
                    }

                    if (data.general_status === 2){
                        $('#general_tips').val('未审批');
                    }else if (data.general_status === 1){
                        $('#general_tips').val('通过');
                        $('#general_time').val(data.general_time);
                    }else if (data.general_status === 3){
                        $('#general_tips').val('拒绝');
                        $('#general_time').val(data.general_time);
                    }
                    if (data.inspector_status === 2){
                        $('#inspector_tips').val('未审批');
                    }else if (data.inspector_status === 1){
                        $('#inspector_tips').val('通过');
                        $('#inspector_time').val(data.inspector_time);
                    }else if (data.inspector_status === 3){
                        $('#inspector_tips').val('拒绝');
                        $('#inspector_time').val(data.inspector_time);
                    }
                    //是否驳回
                    if (data.status === 5){
                        $('#reject_status').show();
                        $('#reject_content_view').val(data.reject_content)
                    }

                    //已审批内展示文件
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/index/PrintingFileAll',
                        data: {
                            'printing_id':data.printing_id
                        },
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            if (data.code === 200){
                                $('#file_content').html(data.data);
                            }else{

                            }
                        }
                    })
                    uploadAll(data.printing_id);

                    table.render({
                        elem: '#articlesDetail'
                        ,height: 312
                        ,where:{
                            printing_id:data.printing_id
                        }
                        ,headers: {
                            Accept: "application/json; charset=utf-8",
                        }
                        ,url: '/general/testzs/public/printing/index/PrintingArticles' //正常表
                        ,cols: [[ //表头
                            { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                            { title: 'articles_id', hide:true, }
                            ,{field: 'value', title: '类别名称', width:150}
                            ,{field: 'name', title: '名称', width:150}
                            ,{field: 'articles_content', title: '内容', width:200}
                            ,{field: 'special', title: '特殊要求及备注', width:150}
                            ,{field: 'articles_edition', title: '印刷版号', width:120}
                            ,{field: 'user_file', title: '附件', width:150}
                        ]]
                        ,response: {
                            statusName: 'code' //规定数据状态的字段名称，默认：code
                            , statusCode: 200 //规定成功的状态码，默认：0
                            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                            , countName: 'count' //规定数据总数的字段名称，默认：count
                            , dataName: 'data' //规定数据列表的字段名称，默认：data
                        }
                    });

                },end : function() {
                    //回调后结束操作
                }




            });
        }
    });
    function uploadAll(printing_id){

        var uploadListIns = upload.render({
            elem: '#testList'
            ,elemList: $('#demoList') //列表元素对象
            ,url: '/general/testzs/public/printing/Sales/SalesUpload'
            ,accept: 'file'
            ,data:{
                'printing_id':printing_id
            }
            ,multiple: true
            ,number: 3
            ,auto: false
            ,bindAction: '#testListAction'
            ,choose: function(obj){
                var that = this;
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td><div class="layui-progress" lay-filter="progress-demo-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    that.elemList.append(tr);
                    element.render('progress'); //渲染新加的进度条组件
                });
            }
            ,done: function(res, index, upload){ //成功的回调
                var that = this;
                if(res.code === 200){ //上传成功
                    layer.alert('上传成功');
                    var tr = that.elemList.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(3).html(''); //清空操作
                    delete this.files[index]; //删除文件队列已经上传成功的文件
                    return;
                }
                this.error(index, upload);
            }
            ,allDone: function(obj){ //多文件上传完毕后的状态回调
                console.log(obj)
            }
            ,error: function(index, upload){ //错误回调
                var that = this;
                var tr = that.elemList.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            ,progress: function(n, elem, e, index){ //注意：index 参数为 layui 2.6.6 新增
                element.progress('progress-demo-'+ index, n + '%'); //执行进度条。n 即为返回的进度百分比
            }
        });
    }

    /* 表格搜索 */
    form.on('submit(SearchForm)', function (data) {
        // 渲染项目表格
        table.render({
            elem: '#printingTable'
            ,headers: {
                Accept: "application/json; charset=utf-8",
            }
            ,where:{
                'printing_number' :data.field.printing_number,
                'customer_name':data.field.customer_name,
                'order_number' :data.field.order_number,
                'status':data.field.status
            }
            ,height: 700
            ,url: '/general/testzs/public/printing/sales/SalesPrinting'
            ,cols: [[ //表头
                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                { title: 'printing_id', hide:true, }
                ,{field: 'printing_number', title: '申请单号'}
                ,{field: 'customer_name', title: '客户名称'}
                ,{field: 'order_number', title: '订单号'}
                ,{field: 'user_name', title: '制单人'}
                ,{field: 'create_time', title: '发单时间'}
                ,{field: 'tips', title: '当前状态'}
                ,{field:'right', title: '操作',toolbar:"#PrintingTool"}
                ,{ title: '采购状态',field:'purchase_status',  hide:true, }
                ,{ title: '总经办状态',field:'general_status', hide:true, }
                ,{ title: '质检部状态',field:'inspector_status', hide:true, }
                ,{ title: '采购审批时间',field:'purchase_time',  hide:true, }
                ,{ title: '总经办审批时间',field:'general_time', hide:true, }
                ,{ title: '质检部审批时间',field:'inspector_time', hide:true, }
                ,{ title: '采购部内容',field:'purchase_content', hide:true, }
                ,{ title: '质检部内容',field:'inspector_content', hide:true, }
            ]]
            ,response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            ,page: true
            ,text: { none: '暂无印刷记录' }
        });
        return false;
    });

});

